package com.mingchuang.administrator.mingchuang;

import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;

import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.exception.DbException;
import com.mingchuang.administrator.mingchuang.bean.PileInfoBean;
import com.mingchuang.administrator.mingchuang.utils.Constant;
import com.mingchuang.administrator.mingchuang.wifi.WifiUtil;
import com.mingchuang.administrator.mingchuang.xml.PileXml;
import com.mingchuang.administrator.mingchuang.zip.FileUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button createWifi;
    private Button carateDB;
    private Button getLocationBtn;
    private Button takePicBtn;
    private Button loginBtn;
    private Button upLoadBtn;
    private Button xmlBtn;
    private Button broadBtn;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        createWifi = (Button)findViewById(R.id.create_wifi);

        carateDB = (Button) findViewById(R.id.create_db);
        final WifiManager wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);

        Constant.getPath();

        createWifi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WifiUtil.stratWifiAp(wifiManager);
            }
        });


        carateDB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               /* //创建数据库
                FinalDb db  = FinalDb.create(MainActivity.this, Constant.DB_NAME);

                iZhuangBean jizhuang = new JiZhuangBean();
                jizhuang.setIsGps("0");
                jizhuang.setName("FIRST JIZHAUNG");
                jizhuang.setIsPic("1");


                db.save(jizhuang);*/
                //创建数据库
                DbUtils db  = DbUtils .create(MainActivity.this, Constant.DB_NAME);

                PileInfoBean jizhuang = new PileInfoBean();
                jizhuang.setIsGps("0");
                jizhuang.setPileName("右20-2");
                jizhuang.setIsPic("1");


                try {
                    db.save(jizhuang);
                } catch (DbException e) {
                    e.printStackTrace();
                }finally {
                    db.close();
                }


            }
        });

        getLocationBtn = (Button) findViewById(R.id.get_location);
        getLocationBtn.setOnClickListener(this);
        takePicBtn = (Button) findViewById(R.id.take_pic);
        takePicBtn.setOnClickListener(this);

        loginBtn = (Button) findViewById(R.id.login_btn);
        loginBtn.setOnClickListener(this);

        upLoadBtn = (Button) findViewById(R.id.upload_btn);
        upLoadBtn.setOnClickListener(this);

        xmlBtn = (Button) findViewById(R.id.xml_btn);
        xmlBtn.setOnClickListener(this);

        broadBtn = (Button) findViewById(R.id.broadcast_btn);
        broadBtn.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.get_location:
                //定位
                Intent intent = new Intent();
                intent.setClass(this, GetLocationActivity.class);
                startActivity(intent);

                break;

            case R.id.take_pic:


                //拍照
                Intent intent2 = new Intent();
                intent2.setClass(this, com.mingchuang.administrator.mingchuang.photo.activity.MainActivity.class);
                startActivity(intent2);

                break;
            case  R.id.login_btn:
                Intent intent1 = new Intent();
                intent1.setClass(this, LoginActivity.class);
                startActivity(intent1);

                break;
            case R.id.upload_btn:

                //Constant.upload("",MainActivity.this);

                break;
            case R.id.xml_btn:
                xmlPull();
                break;

            case R.id.broadcast_btn:
                //发送广播
                Intent br = new Intent();
                br.setAction("mingchuang.file.receive");
                br.putExtra("name","test");
                sendBroadcast(br);


            default:

                break;
        }
    }

    private void xmlPull() {

        FileUtils.read(this, Constant.getPath() + "/unzip/K27+983-1217");

       /* File file = new File(Constant.getPath()+"/右20-2_info.xml");
        InputStream is = null;
        try {
            is = new FileInputStream(file);

            DbUtils db  = DbUtils .create(MainActivity.this, Constant.DB_NAME);

            PileInfoBean pile = PileXml.readXML(is);
            db.save(pile);
            db.close();



        } catch (Exception e) {
            e.printStackTrace();

    }*/
    }
}
